package main

import (
	"fmt"
	lib "im/libs"
	protos "im/pb"
	"net"
	"time"
)

func ScanLine() string {
	var c byte
	var err error
	var b []byte
	for ; err == nil; {
		_, err = fmt.Scanf("%c", &c)

		if c != '\n' {
			b = append(b, c)
		} else {
			break;
		}
	}

	return string(b)
}

func GateWay(end chan bool) {
	conn, err := net.Dial("tcp", "127.0.0.1:1000")
	if err != nil {
		fmt.Printf("connect error : %v\n", err)
		return
	}

	r := &protos.Request{Command: protos.Command_PING}
	data, err := lib.EncodeReq(r)
	conn.Write(data)
	//fmt.Printf("send ping\n")
	if err != nil {
		fmt.Printf("send error : %v\n", err)
		return
	}
	rsp, _ := lib.ReadRsp(conn)
	//fmt.Printf("<===%v\n", rsp)
	r = &protos.Request{Command: protos.Command_AUTH}
	r.Timestamp = uint64(time.Now().Unix())
	data, err = lib.EncodeReq(r)
	//fmt.Printf("send auth %v\n", data)
	conn.Write(data)
	if err != nil {
		fmt.Printf("send error : %v\n", err)
		return
	}
	rsp, _ = lib.ReadRsp(conn)
	//fmt.Printf("<===%v\n", rsp)
	defer conn.Close()

	conn1, err := net.Dial("tcp", fmt.Sprintf("%s:%d", rsp.Host, rsp.Port))
	if err != nil {
		fmt.Printf("connect error : %v\n", err)
		return
	}
	//time.Sleep(time.Duration(1) * time.Second)
	r1 := &protos.Request{Command: protos.Command_AUTH}
	r1.Username = "admin"
	r1.Password = "123"

	fmt.Printf("Username>")
	r1.Username = ScanLine()
	fmt.Printf("Password>")
	r1.Password = ScanLine()

	data, err = lib.EncodeReq(r1)
	conn1.Write(data)

	if err != nil {
		fmt.Printf("send error : %v\n", err)
		return
	}
	rsp1, _ := lib.ReadRsp(conn1)
	//fmt.Printf("token %v ,send error : %v\n", rsp1, err)
	conn1.Close()

	r = &protos.Request{Command: protos.Command_CHAT}
	r.Timestamp = uint64(time.Now().Unix())
	data, err = lib.EncodeReq(r)
	//fmt.Printf("send auth %v\n", data)
	conn.Write(data)
	if err != nil {
		fmt.Printf("send error : %v\n", err)
		return
	}
	rsp, _ = lib.ReadRsp(conn)
	//fmt.Printf("<===%v\n", rsp)
	conn1, err = net.Dial("tcp", fmt.Sprintf("%s:%d", rsp.Host, rsp.Port))
	if err != nil {
		fmt.Printf("connect error : %v\n", err)
		return
	}
	go func() {
		for{
			rsp, e := lib.ReadRsp(conn1)
			if e !=nil {
				fmt.Printf("read error %v\n",e)
				continue
			}
			//fmt.Printf("get what:%+v\n",rsp)
			switch rsp.Command {
			case protos.Command_PULL_MSG:
				if rsp.Msg != nil {
					fmt.Printf("[%d]=>%s\n",rsp.Msg.Uid,rsp.Msg.Msg)
				}
			}
		}
	}()

	go func() {
		for{
			req := &protos.Request{}
			req.Command=protos.Command_PULL_MSG
			req.Username = "admin"
			req.Timestamp = uint64(time.Now().Unix())
			req.Token = rsp1.Token
			data, err = lib.EncodeReq(req)
			conn1.Write(data)
			time.Sleep(time.Second)
		}
	}()

	for{
		s := ScanLine()
		req := &protos.Request{}
		req.Command=protos.Command_SEND_MSG
		req.Msg = s
		req.Username = "admin"
		req.Timestamp = uint64(time.Now().Unix())
		req.Token = rsp1.Token
		data, err = lib.EncodeReq(req)
		//fmt.Printf("send chat %v\n", req)
		conn1.Write(data)
		fmt.Printf("[me]=>%s\n", s)
	}
}
func main() {
	end := make(chan bool)
	GateWay(end)
}
